﻿using EasyCore.App;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.Configurations.Extensions
{
    /// <summary>
    /// 配置扩展类
    /// </summary>
    public static class ConfigurationExtensions
    {
        /// <summary>
        /// 添加配置选项
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddEasyOptions<TOptions>(this IServiceCollection services) where TOptions : class
        {
            var optionsType = typeof(TOptions); //获得当前类型
            var attr = optionsType.GetOptionsSettings(); //获得配置选项设置
            var configuration = EasyApp.Configuration.GetSection(attr.Path); //获得配置节
            services.AddOptions<TOptions>().Bind(configuration, options =>
            {
                options.BindNonPublicProperties = true; //绑定非公共属性
            }).ValidateDataAnnotations().ValidateOnStart(); //绑定配置节并启用数据注解验证
            return services;
        }
    }
}
